package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ICatCategoriesConverter;
import com.cskaoyan.shopping.converter.ITestProductConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.*;
import com.cskaoyan.shopping.dal.persistence.*;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ITestProductService;
import com.cskaoyan.shopping.service.ShoppingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :LuoXuXin
 * @version : $version
 * @date :Created in 2022/4/22   15:07
 * @description:
 * @return:
 */
@Service
public class ShoppingServiceImpl implements ShoppingService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public List<PanelDto> getShoppingHomePage() {
        List<PanelDto> shoppingHomeResponses = new ArrayList<>();
        //查询所有一级类目信息
        //条件查询，只要显示位置在首页的类目
        Example example = new Example(Panel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("position",0);
        List<Panel> panels = panelMapper.selectByExample(example);
        for (Panel panel : panels) {
            Integer panelId = panel.getId();
            PanelDto panelDto = contentConverter.panel2Dto(panel);

            List<PanelContentItem> panelContentItems =
                    panelContentMapper.selectPanelContentAndProductWithPanelId(panelId);
            List<PanelContentItemDto> panelContentItemDtos =
                    contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);
            shoppingHomeResponses.add(panelDto);
        }

        return shoppingHomeResponses;
    }

    @Override
    public List<PanelContentDto> getShoppingNavigation() {
        List<PanelContentDto> panelContentDtos = new ArrayList<>();

        List<PanelContentItem> panelContentItems =
                panelContentMapper.selectPanelContentAndProductWithPanelId(0);
        for (PanelContentItem panelContentItem : panelContentItems) {
            PanelContentDto panelContentDto =
                    contentConverter.panelContentItem2Dto(panelContentItem);
            panelContentDtos.add(panelContentDto);
        }
        return panelContentDtos;
    }

    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ICatCategoriesConverter iCatCategoriesConverter;
    @Override
    public List<ShoppingCategoriesDto> getShoppingCategories(String sort) {
        List<ShoppingCategoriesDto> categoriesDtos = new ArrayList<>();
        Example example = new Example(ItemCat.class);
        if (sort != null && !"".equals(sort)) {
            example.setOrderByClause(sort);
        }
        List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
        for (ItemCat itemCat : itemCats) {
            ShoppingCategoriesDto dto = iCatCategoriesConverter.shoppingCategories(itemCat);
            categoriesDtos.add(dto);
        }
        return categoriesDtos;

    }

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;

    @Override
    public ShoppingProductResponse getProductDetail(ITestProductRequest request) {


        if (request.getProductId() == null) {
            return null;
        } else {
            Item item = itemMapper.selectByPrimaryKey(request.getProductId());
            String[] itemImages = item.getImages();
            String imageBig = item.getImageBig();
            ShoppingProductResponse shoppingProductResponse = productConverter.item2Response(item);
            shoppingProductResponse.setProductImageBig(imageBig);
            shoppingProductResponse.setProductImageSmall(itemImages);
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getProductId());
            shoppingProductResponse.setDetail(itemDesc.getItemDesc());

            return shoppingProductResponse;
        }

    }

    @Override
    public AllProductResponse getShoppingGoodsPage(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        //条件查询
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        if (request.getSort() != null) {
            example.setOrderByClause(request.getSort());
        }
        if (request.getCid() != null) {
            criteria.andEqualTo("cid",request.getCid());
        }
        if (request.getPriceGt() != null) {
            criteria.andGreaterThan("price", request.getPriceGt());
        }
        if (request.getPriceLte() != null) {
            criteria.andLessThan("price", request.getPriceLte());
        }
        List<Item> items = itemMapper.selectByExample(example);
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        //开启分页
        Integer page = request.getPage();
        Integer size = request.getSize();
        PageHelper.startPage(page, size);
        //统计获取到的数据条数
        PageInfo<ProductDto> pageInfo = new PageInfo<>(productDtos);
        long total = pageInfo.getTotal();
        //封装返回信息
        response.setTotal(total);
        response.setData(productDtos);
        return response;
    }

    @Override
    public List<PanelDto> getRecommendGood() {
        List<PanelDto> panelDtos = new ArrayList<>();
        //查询出现在商品推荐板块的一级类目
        Example example = new Example(Panel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("position", 1);
        List<Panel> panels = panelMapper.selectByExample(example);
        //根据一级类目提供的panelId查询PanelContent
        for (Panel panel : panels) {
            Integer panelId = panel.getId();
            //dto缺少contentItemDto
            PanelDto panelDto = contentConverter.panel2Dto(panel);
            //根据panelId查询panelContent
            Example example1 = new Example(PanelContent.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("panelId", panelId);
            List<PanelContent> panelContents =
                    panelContentMapper.selectByExample(example1);
            //panelContentItemDto缺少商品信息
            List<PanelContentItemDto> panelContentItemDtos
                    = contentConverter.panelContent2Dto(panelContents);

            for (PanelContentItemDto panelContentItemDto : panelContentItemDtos) {
                Long productId = panelContentItemDto.getProductId();
                Item item = itemMapper.selectByPrimaryKey(productId);
                panelContentItemDto.setProductName(item.getTitle());
                panelContentItemDto.setSalePrice(item.getPrice());
                panelContentItemDto.setSubTitle(item.getSellPoint());
            }
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDtos.add(panelDto);
        }

        return panelDtos;
    }
}
